package com.china08.yxyapi.api.manage.tec.teaching.scoreport.createexam;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBookContents;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResBookContentsRepository;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResResourceBookContentRepository;

/**
 * 查询当前学科对应的策别 和 策别下面对应的章节(一个接口当成两个接口用)
 * 
 * @author yyy
 *
 */
@CrossOrigin
@RestController
public class CreateExamChapterController extends YxyApiBaseController {
    @Autowired
    private YxyResBookRepository yxyResBookRepository;
    @Autowired
    private YxyResResourceBookContentRepository yxyResResourceBookContentRepository;
    @Autowired
    private YxyResBookContentsRepository yrbcr;

    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/createexam/charpter/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        RespModel model = new RespModel();
        String[] subjectName = reqModel.subjectName.split("-");
        String[] version = reqModel.versionId.split("-");
        
        List<Object[]> versonLst = yxyResBookRepository.getVerson(subjectName[1], subjectName[0], version[0]);

        List<VersionModel> versionList = new ArrayList<VersionModel>();
        VersionModel versionModel = null;
        for (Object[] objects : versonLst) {
            versionModel = new VersionModel();
            versionModel.versionId = objects[0].toString();
            versionModel.versionName = objects[1].toString();
            versionList.add(versionModel);
        }

        if (reqModel.versionId != null && !reqModel.versionId.equals("")) {
        	String[] semesterId=reqModel.semesterId.split("-");
            BookContentRespModel bookContentRespModel = null;
            List<BookContentRespModel> bookContentRespModelLst = new ArrayList<BookContentRespModel>();
            List<Object> charpterList = yxyResResourceBookContentRepository.getCharpter(subjectName[0],
            		semesterId[0], subjectName[1], version[0]);
            for (Object objects : charpterList) {
                List<YxyResBookContents> listcoContents = yrbcr.findByBookIdAndIsViewOrderByOrderNum(objects.toString(),
                        1);
                for (YxyResBookContents yxyResBookContents : listcoContents) {
                    bookContentRespModel = new BookContentRespModel();
                    bookContentRespModel.id = yxyResBookContents.getId();
                    bookContentRespModel.name = yxyResBookContents.getName();
                    bookContentRespModel.pId = yxyResBookContents.getpId();
                    bookContentRespModelLst.add(bookContentRespModel);
                }
            }
            model.bookContent = bookContentRespModelLst;
        }
        model.versionList = versionList;

        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
        // 版本id和name
        public List<VersionModel> versionList;
        // 章节id、章节父级id、章节name
        public List<BookContentRespModel> bookContent;

        public List<BookContentRespModel> getBookContent() {
            return bookContent;
        }

        public void setBookContent(List<BookContentRespModel> bookContent) {
            this.bookContent = bookContent;
        }
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 学段名称
        public String subjectName;
        // 策别id
        public String semesterId;
        // 版本id
        public String versionId;
    }

    protected class VersionModel {
        // 版本id
        public String versionId;
        // 版本name
        public String versionName;
    }

    protected static class BookContentRespModel {
        public String id;
        public String pId;
        public String name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getpId() {
            return pId;
        }

        public void setpId(String pId) {
            this.pId = pId;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

    }
}
